/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans;

import entities.Candidate;
import entities.CandidateConfiguration;
import entities.Company;
import entities.CompanyConfiguration;
import entities.Language;
import entities.PublicRelations;
import entities.User;
import entities.UserConfiguration;
import facades.CandidateConfigurationFacade;
import facades.CandidateFacade;
import facades.CompanyConfigurationFacade;
import facades.CompanyFacade;
import facades.LanguageFacade;
import facades.UserConfigurationFacade;
import facades.UserFacade;
import java.io.Serializable;
import java.util.Locale;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.Event;
import javax.enterprise.event.Observes;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import qualifiers.AdminCandidateEvent;
import qualifiers.CreatedEntityEvent;
import qualifiers.EditedEntityEvent;
import qualifiers.LoggedIn;
import qualifiers.PreCreateEntityEvent;

/**
 *
 * @author Alvaro
 */
@ApplicationScoped
public class ConfigurationBean implements Serializable {
    @Inject
    @CreatedEntityEvent
    private Event<CompanyConfiguration> createCompanyConfigurationEvent;
    @Inject
    @PreCreateEntityEvent
    private Event<CompanyConfiguration> preCreateCompanyConfigurationEvent;
    @Inject
    @CreatedEntityEvent
    private Event<CandidateConfiguration>createCandidateConfigurationEvent;
    @Inject
    @PreCreateEntityEvent
    private Event<CandidateConfiguration> preCreateCandidateConfigurationEvent;
    @Inject
    @PreCreateEntityEvent
    private Event<UserConfiguration> preCreateUserConfigurationEvent;
    @Inject
    @CreatedEntityEvent
    private Event<UserConfiguration> createUserConfigurationEvent;
    @Inject
    private LanguageFacade languageFacade;
    @Inject
    private UserConfigurationFacade userConfigurationFacade;
    @Inject
    private UserFacade userFacade;
    @Inject 
    private CandidateFacade candidateFacade;
    @Inject
    private CandidateConfigurationFacade candidateConfigurationFacade;
    @Inject
    private CompanyFacade companyFacade;
    @Inject
    private CompanyConfigurationFacade companyConfigurationFacade;
    @Inject
    @EditedEntityEvent
    private Event<Candidate> editedCandidateEvent;
    @Inject
    @EditedEntityEvent
    private Event<User> editedUserEvent;
    @Inject
    @EditedEntityEvent
    private Event<Company> editedCompanyEvent;
    /**
     * Creates a new instance of Configuration
     */
    public ConfigurationBean() {
    }
    
    
    public void onNewCandidate(@Observes @CreatedEntityEvent Candidate candidate){
        if(candidate.getCandidateConfiguration()==null){
            createCandidateConfiguration(candidate);
        }
    }
    
    public void onNewUser(@Observes @CreatedEntityEvent User user){
        if(user.getUserConfiguration()==null){
            createUserConfiguration(user);
        }
    }
    public void onNewCompany(@Observes @CreatedEntityEvent Company company){
        if(company.getCompanyConfiguration()==null){
            createCompanyConfiguration(company);
        }
    }
    
     public void onLoggedInCompany(@Observes @LoggedIn Company company){
        if(company.getIdUser().getUserConfiguration() == null){
            createUserConfiguration(company.getIdUser());
        }
        if(company.getCompanyConfiguration() == null){
            createCompanyConfiguration(company);
        }
     }
     
    public void onLoggedInCandidate(@Observes @LoggedIn Candidate candidate){
        if(candidate.getUser().getUserConfiguration() == null){
            createUserConfiguration(candidate.getUser());
        }
        if(candidate.getCandidateConfiguration() == null){
            createCandidateConfiguration(candidate);
        }
     }
    
    public void onAdminViewCandidate(@Observes @AdminCandidateEvent Candidate candidate){
        if(candidate.getUser().getUserConfiguration() == null){
            createUserConfiguration(candidate.getUser());
        }
        if(candidate.getCandidateConfiguration() == null){
            createCandidateConfiguration(candidate);
        }
     }
     
    public void onLoggedInPublicRelations(@Observes @LoggedIn PublicRelations publicRelations){
        if(publicRelations.getUser().getUserConfiguration() == null){
            createUserConfiguration(publicRelations.getUser());
        }
     }
    
     private void createUserConfiguration(User user){
        UserConfiguration userConfiguration= new UserConfiguration();
        userConfiguration.setUser(user);
        Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
        Language language = languageFacade.find(locale);
        if(language==null){
            language = languageFacade.getEnglish();
        }
        userConfiguration.setLanguage(language);
        preCreateUserConfigurationEvent.fire(userConfiguration);
        userConfigurationFacade.create(userConfiguration);
        createUserConfigurationEvent.fire(userConfiguration);
        user.setUserConfiguration(userConfiguration);
        //editedUserEvent.fire(user);
        //userFacade.evict(user, user.getIdUser());
     }
     
     private void createCompanyConfiguration(Company company){
        CompanyConfiguration companyConfiguration = new CompanyConfiguration();
        companyConfiguration.setCompany(company);
        companyConfiguration.setFacesPerRow(4);
        companyConfiguration.setShowAdquiredProfiles(true);
        companyConfiguration.setShowName(true);
        preCreateCompanyConfigurationEvent.fire(companyConfiguration);
        companyConfigurationFacade.create(companyConfiguration);
        company.setCompanyConfiguration(companyConfiguration);
        //company.getIdUser().getCompany().setCompanyConfiguration(companyConfiguration);
        createCompanyConfigurationEvent.fire(companyConfiguration);
        //editedCompanyEvent.fire(company);
        //editedUserEvent.fire(company.getIdUser());
        //companyFacade.evict(company, company.getIdCompany());
        //userFacade.evict(company.getIdUser(), company.getIdUser().getIdUser());
     }
     private void createCandidateConfiguration(Candidate candidate){
        CandidateConfiguration candidateConfiguration= new CandidateConfiguration();
        candidateConfiguration.setCandidate(candidate);
        candidateConfiguration.setShowOffers(true);
        candidateConfiguration.setEmailAdquired(true);
        candidateConfiguration.setEmailVideo(true);
        candidateConfiguration.setEmailVisited(true);
        candidateConfiguration.setEmailNewSearch(true);
        preCreateCandidateConfigurationEvent.fire(candidateConfiguration);
        candidateConfigurationFacade.create(candidateConfiguration);
        candidate.setCandidateConfiguration(candidateConfiguration);
        //candidate.getUser().getCandidate().setCandidateConfiguration(candidateConfiguration);
        createCandidateConfigurationEvent.fire(candidateConfiguration);
        //editedCandidateEvent.fire(candidate);
        //editedUserEvent.fire(candidate.getUser());
        //candidateFacade.evict(candidate, candidate.getIdCandidate());
        //userFacade.evict(candidate.getUser(), candidate.getUser().getIdUser());
     }
}
